package com.yuke.cloud.service.omc.controller.frontend;

import com.yuke.cloud.common.util.wrapper.WrapMapper;
import com.yuke.cloud.common.util.wrapper.Wrapper;
import com.yuke.cloud.service.omc.dto.OdsAwardRecordDto;
import com.yuke.cloud.service.omc.dto.OdsAwardRecordListDto;
import com.yuke.cloud.service.omc.entity.OdsAwardRecord;
import com.yuke.cloud.service.omc.service.OdsAwardRecordService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.web.bind.annotation.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.http.MediaType;
import com.yuke.cloud.common.core.support.BaseController;

import javax.annotation.Resource;
import java.util.List;

/**
* Created by zn on 2020/10/17.
*/
@Api(value = "WEB - OdsAwardRecordController", tags = "处理OdsAwardRecord相关请求", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
@RestController
@RequestMapping("/ods/awardRecord")
public class OdsAwardRecordController extends BaseController {
    @Resource
    private OdsAwardRecordService odsAwardRecordService;

    @PostMapping
    @ApiOperation(httpMethod = "POST", value = "新增一条记录")
    public Wrapper add(@ApiParam(name = "odsAwardRecord", value = "该数据记录的POJO") @RequestBody OdsAwardRecord odsAwardRecord) {
        odsAwardRecord.setRecordId(generateId());
        odsAwardRecordService.saveRecord(odsAwardRecord);
        return WrapMapper.ok();
    }

    @DeleteMapping("/{id}")
    @ApiOperation(httpMethod = "DELETE", value = "根据id删除一条记录")
    public Wrapper delete(@ApiParam(name = "id", value = "记录id") @PathVariable Long id) {
        odsAwardRecordService.deleteById(id);
        return WrapMapper.ok();
    }

    @PutMapping
    @ApiOperation(httpMethod = "PUT", value = "修改一条记录")
    public Wrapper update(@ApiParam(name = "odsAwardRecord", value = "需要修改的内容") @RequestBody OdsAwardRecord odsAwardRecord) {
        odsAwardRecordService.update(odsAwardRecord);
        return WrapMapper.ok();
    }

    @GetMapping("/{id}")
    @ApiOperation(httpMethod = "GET", value = "根据ID获取具体信息")
    public Wrapper<OdsAwardRecord> detail(@ApiParam(name = "id", value = "记录id") @PathVariable Long id) {
        OdsAwardRecord odsAwardRecord = odsAwardRecordService.findById(id);
        return WrapMapper.ok(odsAwardRecord);
    }

    @GetMapping
    @ApiOperation(httpMethod = "GET", value = "分页获取所有记录")
    public Wrapper<PageInfo<OdsAwardRecord>> list(@ApiParam(name = "page", value = "分页值", defaultValue = "0") @RequestParam(defaultValue = "0") Integer page,
                                                    @ApiParam(name = "size", value = "每分页返回的记录数", defaultValue = "0") @RequestParam(defaultValue = "0") Integer size) {
        PageHelper.startPage(page, size);
        List<OdsAwardRecord> list = odsAwardRecordService.findAll();
        PageInfo pageInfo = new PageInfo(list);
        return WrapMapper.ok(pageInfo);
    }


    @PostMapping("/selectAwardRecord")
    @ApiOperation(httpMethod = "POST", value = "分页获取所有记录")
    public Wrapper<PageInfo<OdsAwardRecord>> selectAwardRecordByCondition(@ApiParam(name = "page", value = "分页值", defaultValue = "0") @RequestParam(defaultValue = "0") Integer page,
                                                  @ApiParam(name = "size", value = "每分页返回的记录数", defaultValue = "0") @RequestParam(defaultValue = "0") Integer size,
                                                    @RequestBody OdsAwardRecord odsAwardRecord
    ) {
        System.out.println(odsAwardRecord);
        PageHelper.startPage(page, size);
        List<OdsAwardRecordDto> list = odsAwardRecordService.selectAwardRecordByCondition(odsAwardRecord);
        PageInfo pageInfo = new PageInfo(list);
        if (list== null || list.size() < 1){
            return WrapMapper.ok(pageInfo);
        }
        list = odsAwardRecordService.getExtraInfo(list);
        pageInfo.setList(list);
        return WrapMapper.ok(pageInfo);
    }

    /**
     * 批量添加记录
     * @param odsAwardRecordListDto
     * @return
     */
    @PostMapping("/batchAdd")
    @ApiOperation(httpMethod = "POST", value = "新增一条记录")
    public Wrapper batchAdd(@ApiParam(name = "odsAwardRecord", value = "该数据记录的POJO") @RequestBody OdsAwardRecordListDto odsAwardRecordListDto) {
        if (odsAwardRecordListDto == null || odsAwardRecordListDto.getOdsAwardRecordList() == null ||
                odsAwardRecordListDto.getOdsAwardRecordList().size() < 1){
            return WrapMapper.error("参数错误！");
        }

        List<OdsAwardRecord> odsAwardRecordList = odsAwardRecordListDto.getOdsAwardRecordList();
        for (OdsAwardRecord odsAwardRecord : odsAwardRecordList){
            odsAwardRecord.setRecordId(generateId());
            odsAwardRecordService.saveRecord(odsAwardRecord);
        }

        return WrapMapper.ok();
    }
}
